<template>
  <Modal v-model="modal" :title="title" @on-cancel="onCancel">
    <div class="tag-modal">
      <Form :label-width="140" :model="form" ref="form" :rules="rules">
        <FormItem label="周期:" prop="day">
          <InputNumber :max="999999" :min="1" v-model="form.day"></InputNumber>
          <span>天</span>
        </FormItem>
      </Form>
    </div>
    <div slot="footer">
      <Button @click="cancel" size="large" type="text">取消</Button>
      <Button :disabled="btnDisabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  data() {
    const validateNum = (rule, value, callback) => {
      if (value == 0 || value == null || (!/^\d+$/.test(value))) {
          callback(new Error('请输入天数'))
      } else {
        if (parseInt(value) < 0) {
          callback(new Error('输入天数不能为负数'))
        } else {
          callback()
        }
      }
    }
    return {
      modal: false,
      title: '策略设置',
      btnDisabled: false,
      form: {
        day: ''
      },
      rules: {
        day: [
          {
            required: true,
            type: 'number',
            trigger: 'blur',
            validator: validateNum
          }
        ]
      }
    }
  },
  mounted(){
  },
  methods: {
    openModal() {
      this.getData()
      this.modal = true
    },
    onCancel() {
      this.modal = false
    },
    cancel() {
      this.modal = false
    },
    getData() {
      this.status = 'loading'
      this.$ajaxGet('/salesman/config/find_day').then(res => {
        // if (res) {
        //   this.form.day = res
        // } else {
        //   this.form.day = ''
        // }
        this.form.day = res
        this.status = 'pass'
      }).catch(err => {
        this.status = 'error'
      })
    },
    onSave() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.btnDisabled) return
          this.btnDisabled = true
          let param = {
            day: this.form.day
          }
          let url = '/salesman/config/update_day'
          this.$ajaxPost(url, param).then(data => {
            this.$Notice.success({
              title: '保存成功'
            })
            this.$emit('complete')
            this.onCancel()
            this.btnDisabled = false
          }).catch(() => {
            this.btnDisabled = false
          })
        }
      })
    }
  },
  // watch: {
  //   'form.day'(v) {
  //     if (v == 0 || v) {
  //       this.form.day = v + ''
  //     } else {
  //       this.form.day = ''
  //     }
  //   }
  // }
}
</script>